﻿using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

public class UIMenuAddCharacterPartyCtrl : MonoBehaviour {

    public static UIMenuAddCharacterPartyCtrl SetComponent(GameObject gameObject, AddDelegate addDelegate, 
                                    string textInfo, PartyController selectedParty = null)
    {
        UIMenuAddCharacterPartyCtrl instance = gameObject.GetComponent<UIMenuAddCharacterPartyCtrl>();
        instance.addDelegate = addDelegate;
        instance.textInfo = textInfo;
        instance.selectedParty = selectedParty;
        return instance;
    }

    public delegate void AddDelegate(UICharacterItem[] selectedCharacter);
    public UIButton btnAdd;
    public UIButton btnCancel;
    public UILabel lblInfo;
    public UITable tableCharacters;
    public UILabel lblSelezionati;
    public UILabel lblErrore;
    public UICharacterItem characterItemPrefab;

    private bool requireRefresh = false;
    private AddDelegate addDelegate;
    private string textInfo = "Impostare una descrizione";
    private PartyController selectedParty;
    private List<UICharacterItem> listItems = new List<UICharacterItem>();
    private List<UICharacterItem> listSelected = new List<UICharacterItem>();
    private GameObject voxelWorld;
    private FrenzyPeopleController peopleCtrl;

    void Start () {
        
        voxelWorld = GameObject.FindGameObjectWithTag("VoxelWorld");
        peopleCtrl = voxelWorld.GetComponent<FrenzyPeopleController>();

        LoadPeople();

        requireRefresh = true;
    }

    public void Update()
    {
        if( requireRefresh )
        {
            lblInfo.text = textInfo;

            int peopleInParty = selectedParty != null ? selectedParty.PeopleInParty.Count : 0;
            if ( listSelected.Count + peopleInParty > peopleCtrl.SizeParty )
            {
                btnAdd.isEnabled = false;
                lblSelezionati.text = "Selezionati: " + listSelected.Count;
                lblErrore.enabled = true;
                lblErrore.text = "La dimensione della comitiva supera il numero consentito (" + peopleCtrl.SizeParty + ")";
            }
            else
            {
                btnAdd.isEnabled = true;
                lblErrore.enabled = false;
                lblSelezionati.text = "Selezionati: " + listSelected.Count;
            }
        }
    }

    // carica solo le persone che non hanno una comitiva
    public void LoadPeople()
    {
        SteveBrain[] listBrain = (from SteveBrain brain in peopleCtrl.SteveList
                                  where brain.isPartyMember == false
                                  select brain).ToArray<SteveBrain>();

        foreach (SteveBrain item in listBrain)
        {
            GameObject newgame = Instantiate(characterItemPrefab.gameObject);
            UICharacterItem newcharItem = UICharacterItem.CreateOrSetComponent(newgame, item, peopleCtrl, true, false);
            newgame.SetActive(true);

            //lo posiziono all'interno della tabella
            newgame.transform.parent = tableCharacters.transform;
            newgame.transform.localPosition = Vector3.zero;
            newgame.transform.localScale = Vector3.one;

            EventDelegate ed = new EventDelegate(this, "Item_Click");
            ed.parameters[0].value = newcharItem;
            newgame.GetComponent<UIButton>().onClick.Add( ed );

            listItems.Add(newcharItem);
        }

        tableCharacters.Reposition();
    }

    // evento associato al click sul bottone
    public void Item_Click(UICharacterItem selected)
    {
        //aggiungo o rimuovo l'elemento cliccato alla lista dei selezionati
        if (selected.IsSelected == true)
            listSelected.Add(selected);
        else
            listSelected.Remove(selected);
    }

    public void Chiudi_Click()
    {
        Destroy(this.gameObject);
    }

    public void Aggiungi_Click()
    {
        this.addDelegate(listSelected.ToArray());
        Destroy(this.gameObject);
    }
}
